package com.mediacloud.app.newsmodule.fragment.baoliao.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mediacloud.app.newsmodule.model.baoliao.list.list.BaoNiaoListItem;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.activity.microlive.BaseRecyclerAdapterX;
import com.mediacloud.app.newsmodule.fragment.SimpleBottomSheetDialog;
import com.mediacloud.app.newsmodule.fragment.baoliao.holder.BaoLiaoRankListImgVideoHolder;
import com.mediacloud.app.newsmodule.fragment.baoliao.holder.BaoLiaoRankListPureTxtHolder;
import com.mediacloud.app.newsmodule.utils.BaoLiaoItemMoreHandler;
import com.mediacloud.app.newsmodule.utils.GridLayoutSpanCountUtils;
import com.utils.DisplayUtil;

/***
 * 爆料排行列表适配器
 */
public class BaoLiaoRankListAdapter extends BaseRecyclerAdapterX<BaoNiaoListItem> {
    public SimpleBottomSheetDialog.ItemClickListener simpleBtmItemClickListener;

    private float widthPercent = 1.0f;
    private boolean isRegularHeight = false;

    public BaoLiaoRankListAdapter(Context context) {
        super(context);
    }

    public void setWidth(float percent) {
        widthPercent = percent;
    }

    public void setRegularHeight(boolean isRegularHeight) {
        this.isRegularHeight = isRegularHeight;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Log.w("BaoLiaoRankListAdapter", "onCreateViewHolder");
        BaoLiaoRankListPureTxtHolder baoLiaoRankListPureTxtHolder;
        int viewModel = GridLayoutSpanCountUtils.ItemTypeSpec.getMode(viewType);
        viewType = GridLayoutSpanCountUtils.ItemTypeSpec.getSize(viewType);
        if (viewType == ALL) {
            if (isRegularHeight){
                baoLiaoRankListPureTxtHolder = new BaoLiaoRankListImgVideoHolder(LayoutInflater.from(getContext()).inflate(R.layout.baoniao_ranklist_videoimgitem2, parent, false), viewModel);
            }else {
                baoLiaoRankListPureTxtHolder = new BaoLiaoRankListImgVideoHolder(LayoutInflater.from(getContext()).inflate(R.layout.baoniao_ranklist_videoimgitem, parent, false), viewModel);
            }
        } else {
            if (isRegularHeight){
                baoLiaoRankListPureTxtHolder = new BaoLiaoRankListPureTxtHolder(LayoutInflater.from(getContext()).inflate(R.layout.baoniao_ranklist_purtextitem2, parent, false));
            }else {
                baoLiaoRankListPureTxtHolder = new BaoLiaoRankListPureTxtHolder(LayoutInflater.from(getContext()).inflate(R.layout.baoniao_ranklist_purtextitem, parent, false));
            }
        }

        baoLiaoRankListPureTxtHolder.moreIcon.setTag(baoLiaoRankListPureTxtHolder);
        baoLiaoRankListPureTxtHolder.moreIcon.setOnClickListener(this);
        baoLiaoRankListPureTxtHolder.rankTv.setVisibility(View.VISIBLE);

//        设置显示宽度
        ViewGroup.LayoutParams params = baoLiaoRankListPureTxtHolder.cardView.getLayoutParams();
        float width = getContext().getResources().getDisplayMetrics().widthPixels;
        params.width = (int) (width * widthPercent);
        if (isRegularHeight){
            params.height = DisplayUtil.dip2px(context, 359f);
        }else {
            params.height = RecyclerView.LayoutParams.WRAP_CONTENT;
        }
        baoLiaoRankListPureTxtHolder.cardView.setLayoutParams(params);

        return baoLiaoRankListPureTxtHolder;
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Log.w("BaoLiaoRankListAdapter", "onBindViewHolder");
        super.onBindViewHolder(holder, position);
        View baoliaobiaoti = ((BaoLiaoRankListPureTxtHolder) holder).baoniaobiaoti;
        baoliaobiaoti.setTag(R.id.recyclerItemTag, position);
        if (!baoliaobiaoti.hasOnClickListeners()) {
            baoliaobiaoti.setOnClickListener(this);
        }

        BaoliaoInterface anInterface = (BaoliaoInterface) holder;
        anInterface.updateData(getItem(position), position, context,isRegularHeight);
    }

    public int currentClickMorePosition = -1;

    @Override
    public void onClick(View v) {
        super.onClick(v);
        BaoLiaoRankListPureTxtHolder baoLiaoRankListPureTxtHolder = v.getTag() instanceof BaoLiaoRankListPureTxtHolder ? (BaoLiaoRankListPureTxtHolder) v.getTag() : null;
        if (baoLiaoRankListPureTxtHolder != null) {
            Object tag = baoLiaoRankListPureTxtHolder.itemView.getTag(R.id.recyclerItemTag);
            if (tag == null)
                return;
            int position = tag != null && tag instanceof Integer ? (int) tag : -1;
            currentClickMorePosition = position;
            BaoLiaoItemMoreHandler.showMoreAction(v.getContext(), simpleBtmItemClickListener,getItem(position));
        }
    }

    @Override
    public int getItemViewType(int position) {
        BaoNiaoListItem obk = getItem(position);
        if (obk.getVideo() != null && obk.getVideo().size() > 0 || obk.getImage() != null && obk.getImage().size() > 0) {
            int size = obk.baoNiaoMediaList == null ? 0 : obk.baoNiaoMediaList.size();
            return GridLayoutSpanCountUtils.ItemTypeSpec.makeItemTypeSpec(size, ALL);
        }
        return GridLayoutSpanCountUtils.ItemTypeSpec.makeItemTypeSpec(GridLayoutSpanCountUtils.ItemTypeSpec.MODEL_NO_ONE, TXT);
    }

    /**
     * 1 纯文本
     * 2 图文 视频之类的
     */
    public static final int TXT = 1, ALL = 2;

    /**
     *
     */
    public interface BaoliaoInterface {
        void updateData(BaoNiaoListItem item, int position, Context context,boolean isRegular);
    }
}